<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>区划列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiAdmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiAdmin/style/admin.css" media="all">
    <style>
        .demo-tree-box {
            padding: 10px;
            overflow: auto;
            overflow-x: hidden;
        }

        .tree-txt-active {
            background-color: lightblue;
        }

        #test2 {
            /* border: 1px solid #e6e6e6; */
            padding: 10px 5px;
            overflow: auto;
            height: -webkit-calc(100vh - 200px);
            height: -moz-calc(100vh - 200px);
            height: calc(100vh - 200px);
        }
    </style>
</head>

<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto" >
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="text" name="code" placeholder="请输入区划代码" autocomplete="new-password"
                               class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <input type="text" name="name" placeholder="请输入区划名称" autocomplete="new-password"
                               class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="form-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
            <div class="layui-card-body">
                <div class="layui-row">
                    <div class="layui-col-sm2 layui-hide-xs">
                        <div id="test2" class="demo-tree demo-tree-box"></div>
                    </div>
                    <div class="layui-col-sm10 layui-col-xs12">
                        <table id="districts-manage" lay-filter="districts-manage"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="../../layuiAdmin/layui/layui.js"></script>
    <script>
        layui.config({
            base: '../../layuiAdmin/', //静态资源所在路径
            version: top.layui.setter.version
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form', 'table', 'tree', 'util'], function () {
            var $ = layui.$,
                form = layui.form,
                table = layui.table,
                tree = layui.tree,
                util = layui.util,
                admin = layui.admin;
            window.areaName = "districts-manage";
            var tree_data = [];
            admin.req({
                type: 'get',
                async: false,
                url: '/api/Address/GetAllRegions',
                success: function (r) {
                    if (r.code == 200) {
                        tree_data = r.data;
                    }
                }
            });
            //仅节点左侧图标控制收缩
            tree.render({
                elem: '#test2',
                data: tree_data,
                onlyIconControl: true,
                click: function (obj) {
                    //if(obj.data.children.length == 0) return;
                    table.reload(areaName, {
                        where: {
                            queryJson: JSON.stringify({ parentId: obj.data.id })
                        }
                    })
                }
            });
            $("#test2 .layui-tree-txt").click(function (e) { // 在页面任意位置点击而触发此事件
                if ($(e.target).attr('class') === "layui-tree-txt") { // 防止因为点击展开按钮把已选中的样式取消
                    $(".layui-tree-txt").removeClass("tree-txt-active"); // 移除点击样式
                    $(e.target).addClass("tree-txt-active"); // e.target表示被点击的目标
                }
            });
            //监听搜索
            form.on('submit(form-search)', function (data) {
                var field = data.field;
                //执行重载
                table.reload(areaName, {
                    where: { queryJson: JSON.stringify(field) },
                    page: {
                        curr: 1
                    }
                });
            });
            //终端列表
            table.render({
                elem: '#' + areaName,
                toolbar: $.getToolButtons(),
                url: '/api/Address/GetRegionPageList',
                cols: [
                    [
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'number', title: '序号', width: 100, align: 'center', type: 'numbers' },
                        { field: 'cRegionName', title: '区域名称', minWidth: 100 },
                        { field: 'cRegionCode', title: '区域编码', minWidth: 100 },
                        { field: 'cLevel', title: '级别', width: 100 },
                        { field: 'cSortCode', title: '排序', width: 100 },
                        { title: '操作', width: 200, align: 'center', fixed: 'right', templet: () => $.getRowButtons() }
                    ]
                ],
                page: true,
                limit: 15,
                height: $(window).height() - $("#x-operation").height() - 110,
                text: '对不起，加载出现异常！'
            });
            $.TableSet(table, areaName);
            var selectRow = null;
            table.on('row(' + areaName + ')', function (obj) {
                selectRow = obj.data;
                obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            });
            var edit = function (id, cParentId) {
                $.OpenTab({
                    type: 2,
                    title: '编辑',
                    content: 'districtsform.html?id=' + id + '&cParentId=' + cParentId,
                    area: ['896px', '562px'],
                    btn: ['确定', '取消']
                });
            }

            var del = function (type) {
                let ids = [];
                if (type == 1 || type == undefined) {
                    if (selectRow == null) return layer.msg('请选择数据');
                    ids = [selectRow.cId];
                } else if (type == 2) {
                    var checkData = table.checkStatus(areaName).data; //得到选中的数据
                    if (checkData.length === 0) return layer.msg('请选择数据');
                    ids = checkData.map(t => t.cId);
                } else {
                    return layer.msg('请选择删除类型');
                }
                layer.confirm('确定删除当前选中的数据吗？', function (index) {
                    admin.req({
                        type: 'post',
                        url: '/api/Address/DelAddress',
                        data: { ids: ids.join(','), },
                        success: function (data) {
                            if (data.code == 200) {
                                table.reload(areaName);
                                layer.close(index);
                                layer.msg('已删除!', { icon: 1, time: 1000 });
                            } else {
                                layer.close(index);
                                layer.msg(data.msg, { icon: 2, time: 1000 });
                            }
                        }
                    });
                });
            }
            //事件
            util.event('lay-active', {
                'x-add': () => edit(),
                'x-delete': () => del(),
                'x-batch-delete': () => del(2),
                'x-edit': () => edit(selectRow.cId, selectRow.cParentId)
            });
        });
    </script>
</body>

</html>